package com.aw.guiguzi.module.manager.msg

import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwEditTextListener
import com.aw.baselib.util.AwPopupwindowUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.MsgTemplateItemBean
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.databinding.ActivityManagerMsgCreateBinding
import com.aw.guiguzi.event.EventRefreshManagerMsgType
import com.aw.guiguzi.module.manager.msg.presenters.MsgCreatePresent
import com.aw.guiguzi.module.manager.msg.views.MsgCreateView
import com.aw.guiguzi.util.AppPopupWindowUtil
import com.aw.guiguzi.widgets.MsgCreatePopupWindow

class MsgCreateActivity :
    AwMvpBindingActivity<MsgCreatePresent?, ActivityManagerMsgCreateBinding?>(),
    MsgCreateView.View {
    private var tv_template: TextView? = null
    private var tv_user: TextView? = null
    private var tv_count: TextView? = null
    private var et_content: EditText? = null
    private var mTemplateList: MutableList<MsgTemplateItemBean> = ArrayList()
    private var mCurrentTemplate: MsgTemplateItemBean? = null
    private var mList: MutableList<UserInfoBean> = ArrayList()
    private var mCurrentUser: UserInfoBean? = null
    private var isSendAll = false
    private var sendCount = 0
    override fun createPresenter(): MsgCreatePresent {
        return MsgCreatePresent(this)
    }

    override fun getViewBinding(): ActivityManagerMsgCreateBinding {
        return ActivityManagerMsgCreateBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        tv_template = findViewById(R.id.tv_template)
        tv_user = findViewById(R.id.tv_user)
        tv_count = findViewById(R.id.tv_count)
        et_content = findViewById(R.id.et_content)
        setToolbar("新增站内信", null)
    }

    override fun initData() {
        super.initData()
        mPresenter!!.getMsgTemplateList()
        //        mPresenter.getUserListSimple();
    }

    override fun initListener() {
        super.initListener()
        tv_template!!.setOnClickListener { v: View? ->
            if (AwDataUtil.isEmpty(mList)) {
                showMsg("模板列表获取失败，请重新进入页面尝试")
                return@setOnClickListener
            }
            AppPopupWindowUtil.showMsgCreatePopupWindow(
                mActivity,
                "请选择消息模板",
                mTemplateList,
                tv_user,
                object : MsgCreatePopupWindow.OnItemClickListener {
                    override fun onClick(bean: MsgTemplateItemBean) {
                        mCurrentTemplate = bean
                        setText(tv_template, mCurrentTemplate!!.name)
                    }

                }
            )
        }
        tv_user!!.setOnClickListener { v: View? ->
            if (AwDataUtil.isEmpty(mList)) {
                showMsg("用户列表获取失败，请重新进入页面尝试")
                return@setOnClickListener
            }
            AwPopupwindowUtil.showCommonListWithTitlePopupWindow(
                mActivity,
                "请选择用户",
                mList,
                tv_user
            ) { bean: Any? ->
                mCurrentUser = bean as UserInfoBean?
                setText(tv_user, mCurrentUser!!.nickname)
            }
        }
        AwEditTextListener.isChangeListener(et_content) { changed: Boolean ->
            setTextNoDefault(
                tv_count,
                getText(et_content).toString().length.toString() + "/200"
            )
        }
        findViewById<View>(R.id.tv_confirm).setOnClickListener { v: View? ->
            if (mCurrentTemplate == null || AwDataUtil.isEmpty(getText(tv_template))) {
                showMsg("请选择消息模板")
                return@setOnClickListener
            }
            if (mCurrentUser == null || AwDataUtil.isEmpty(getText(tv_user))) {
                showMsg("请选择目标用户")
                return@setOnClickListener
            }
            if (AwDataUtil.isEmpty(getText(et_content))) {
                showMsg("请输入发送内容")
                return@setOnClickListener
            }
            isSendAll = false
            sendCount = 0
            if (mCurrentUser!!.id == "-1") {
                //发送所有人
                isSendAll = true
                for (item in mList) {
                    mPresenter!!.msgCreateToSend(
                        item.itemId, mCurrentTemplate!!.itemId, createMsgContent(
                            mCurrentUser!!.nickname, getText(et_content)
                        )
                    )
                }
            } else {
                //单个人发送
                isSendAll = false
                mPresenter!!.msgCreateToSend(
                    mCurrentUser!!.itemId, mCurrentTemplate!!.itemId, createMsgContent(
                        mCurrentUser!!.nickname, getText(et_content)
                    )
                )
            }
        }
    }

    private fun createMsgContent(nickname: String?, content: String): HashMap<String?, String?> {
        val hashMap = HashMap<String?, String?>()
        if (mCurrentTemplate!!.itemId == "10000") {
            hashMap["nickname"] = nickname
        }
        hashMap["content"] = content
        return hashMap
    }

    override fun getMsgTemplateListSuccess(total: Int, list: List<MsgTemplateItemBean>) {
        mTemplateList = list.toMutableList()
    }

    override fun getMsgTemplateListFail(code: Int, msg: String?) {
        mTemplateList = ArrayList()
    }

    override fun getUserListSimpleSuccess(list: List<UserInfoBean>) {
        mList = list.toMutableList()
        val userInfoBean = UserInfoBean()
        userInfoBean.nickname = "所有人"
        userInfoBean.itemId = "-1"
        mList.add(0, userInfoBean)
    }

    private fun sendSuccessNextStep() {
        showMsg("已发送")
        postEvent(EventRefreshManagerMsgType(null))
        finish()
    }

    override fun getUserListSimpleFail(code: Int, msg: String?) {
        mList = ArrayList()
    }

    override fun msgCreateToSendSuccess() {
        sendCount++
        if (isSendAll) {
            if (sendCount == mList.size - 1) {
                sendSuccessNextStep()
            }
        } else {
            sendSuccessNextStep()
        }
    }
}